package com.app.controller.management.producto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.app.json.MarcaResponse;
import com.app.service.management.producto.MarcaService;
import com.app.to.Marca;

@RequestMapping("/mantenimiento")
@Controller
public class MarcaController {

	@Autowired
	private MarcaService marcaService;

	@RequestMapping(value = "/marca", method = RequestMethod.GET)
	public ModelAndView marca() {
		return new ModelAndView("admin/mantenimiento/mantenimientoMarca");
	}

	@RequestMapping(value = "/MarcaGrid", method = RequestMethod.GET)
	public @ResponseBody MarcaResponse MarcaCarga(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {

			MarcaResponse respMarca = new MarcaResponse();
			// List<Map<String, Object>> rows = new ArrayList<Map<String,
			// Object>>();
			List<Marca> marcas = marcaService.getListMarca();

			// FIXME
			/*
			 * com.app.entity.Marca marca = new com.app.entity.Marca();
			 * marca.setDescripcion("prueba ruben"); marca.setEstado("1");
			 * marcaService.saveMarca(marca);
			 */

			//
			for (Marca m : marcas) {
				Map<String, Object> resService = new HashMap<String, Object>();
				resService.put("id", m.getId());
				resService.put("descripcion", m.getDescripcion());
				if (m.getEstado().equals("1")) {
					resService.put("estado", "Activo");
				} else {
					resService.put("estado", "Desactivado");
				}
			}

			respMarca.setRows(marcas);
			int count = (Integer) marcas.size();
			int limit = 10;
			respMarca.setRows(marcas);
			int totalPag = 0;

			if (count > 0) {
				totalPag = (int) Math.ceil(count / limit);
				if (count % limit > 0) {
					totalPag = totalPag + 1;
				}

			} else {
				totalPag = 0;
			}
			respMarca.setRecords(String.valueOf(count));
			respMarca.setPage(String.valueOf(1));
			respMarca.setTotal(String.valueOf(totalPag));
			return respMarca;
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/NuevaMarca", method = RequestMethod.POST)
	public ModelAndView nuevaMarca(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			return new ModelAndView("admin/mantenimiento/nuevaMarca");
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/marcaInsertar", method = RequestMethod.POST)
	public ModelAndView insertarMarca(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			Marca marca = new Marca();
			marca.setDescripcion(request.getParameter("nombre"));
			marca.setEstado("1");
			marcaService.saveMarca(marca);
			System.out.println(marca.getDescripcion());
			return new ModelAndView("admin/mantenimiento/mantinimientoMarca");
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/MarcaEliminar", method = RequestMethod.POST)
	public ModelAndView eliminarMarca(HttpServletRequest request,
			HttpServletResponse response, String codigoMarca) throws Exception {
		try {
			System.out.println(Integer.parseInt(codigoMarca));
			return new ModelAndView("admin/mantenimiento/mantinimientoMarca");
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/MarcaActualizar", method = RequestMethod.POST)
	public ModelAndView actualizarMarca(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			Marca marca = new Marca();
			marca.setId(Long.parseLong(request.getParameter("codigo")));
			marca.setDescripcion(request.getParameter("nombre"));
			System.out.println(marca.getId());
			System.out.println(marca.getDescripcion());
			return new ModelAndView("admin/mantenimiento/mantinimientoMarca");
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/MarcaEditar", method = RequestMethod.POST)
	public ModelAndView editarMarca(HttpServletRequest request,
			HttpServletResponse response, String codigoMarca) throws Exception {
		try {
			Map<String, Object> result = new HashMap<String, Object>();
			System.out.println(Long.parseLong(codigoMarca));
			List<Marca> marcas = marcaService.getListMarca();
			for (Marca m : marcas) {
				result.put("ServicioCodigo", m.getId());
				result.put("ServicioDescripcion", m.getDescripcion());

			}
			return new ModelAndView("admin/mantenimiento/editarMarca",
					"resultMarca", result);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/BuscarMarca", method = RequestMethod.POST)
	public ModelAndView BuscarMarca(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try {
			HttpSession session = request.getSession(true);
			session.setAttribute("sessionDatosMarca",
					request.getParameter("datos"));
			return new ModelAndView("admin/mantenimiento/MantenimientoMarca");
		} catch (Exception e) {
			return null;
		}
	}
}